/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.itschapala.dsw2.Bitacora.vista;


import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.itschapala.dsw2.bitacora.Bl.CarrosBlLocal;
import mx.itschapala.dsw2.bitacora.modelo.Carros;

/**
 *
 * @author Etts
 */
@ManagedBean
@SessionScoped
public class CarrosBean {
    @EJB
    private CarrosBlLocal carrosBl;
    
    private String accion;
    private int n;
    //--------------------------------
    List<Carros>lista;
    Carros carro;
    
    
    

    /**
     * Creates a new instance of CarrosBean
     */
    public CarrosBean() {
        carro=new Carros();
    }
    
    //---------------------------------

    public List<Carros> getLista() {
        lista=carrosBl.getList();
        
        return lista;
    }

    public Carros getCarro() {
        return carro;
    }

    public void setCarro(Carros carro) {
        this.carro = carro;
    }
    
    public void prepararCarro()
    {
    carro=new Carros();
    }
    
    public void agregarCarro()
    {
    carrosBl.register(carro);
    
    }
    
    //-----------------Acciones------------------------------
public String procesarAccion(){
if(accion.equalsIgnoreCase("Nuevo")){
carrosBl.register(carro);
}
else
if(accion.equalsIgnoreCase("Editar")){
carrosBl.Modify(carro);
}
else if(accion.equalsIgnoreCase("Eliminar")){
carrosBl.Delete(carro);


}
accion="";
return "CarrosLista";
}



//-----------------Listener------------------------------

public void prepararNuevo(ActionEvent evt){
carro=new Carros();
accion="Nuevo";
}
public void prepararEditar(ActionEvent evt)
{
System.out.println(evt.getComponent().getClientId());
n=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
System.out.println("el numero es:"+n);
carro=lista.get(n);
accion="Editar";
}

public void prepararEliminar(ActionEvent evt)
{
int x;
System.out.println(evt.getComponent().getClientId());
n=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
System.out.println("el numero es:"+n);
carro=lista.get(n);


accion="Eliminar";
}


    
}
